﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace GaiNo.Core.Modules
{
    public class ExceptionModule : IHttpModule
    {
        #region IHttpModule 成员

        public void Dispose()
        {

        }
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(context_Error);
        }
        void context_Error(object sender, EventArgs e)
        {
            bool debug = false;
#if DEBUG
            debug = true;

#endif
            if (debug)
                return;

            HttpApplication app = sender as HttpApplication;
            string returnUrl = app.Request.RawUrl;

            Exception ex = app.Context.Error;
            if (ex == null)
                ex = app.Context.Error.InnerException;

            if (ex == null)
                return;
            ApplicationContext.Current.Message.Error(ex.Message);
            if (ex is MessageException)
            {
                ApplicationContext.Current.Message.Info(ex.Message);
                app.Context.ClearError();
                app.Context.Response.Redirect(returnUrl);
            }
            else if (ex is NotDisplayedException)
            {
                app.Context.Response.Redirect("~/Error.aspx", true);
            }
        }
        #endregion
    }//end class
}//end namespace
